<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

use Think\Page;
use Think\Verify;

class PublicController extends BaseController {

    /**
     * 管理员登陆
     */
    public function login() {
        if (session('?admin_id') && session('admin_id') > 0) {
            $this->error("您已登录", U('Admin/Index/index'));
        }
        if (IS_POST) {
            $verify = new Verify();
            if (!$verify->check(I('post.vertify'), "admin_login")) {
                exit(json_encode(array('status' => 0, 'msg' => '验证码错误')));
            }
            $where['user_name'] = I('post.username');
            $where['password'] = I('post.password');
            if (!empty($where['user_name']) && !empty($where['password'])) {
                $where['password'] = encrypt($where['password']);
                $adminInfo = M('admin')
                    ->join('__ADMIN_ROLE__ ON __ADMIN__.role_id=__ADMIN_ROLE__.role_id')
                    ->where($where)
                    ->find();
                if (is_array($adminInfo)) {
//                    session(array('admin_id' => $adminInfo['admin_id'], 'expire' => 10));
                    session('admin_id', $adminInfo['admin_id']);
                    session('act_list', $adminInfo['act_list']);
                    $last_login_time = M('admin_log')->where("admin_id = " . $adminInfo['admin_id'] . " and log_info = '后台登录'")->order('log_id desc')->limit(1)->getField('log_time');
                    M('admin')->where("admin_id = " . $adminInfo['admin_id'])->save(array('last_login' => time(), 'last_ip' => getIP()));
                    session('last_login_time', $last_login_time);
                    adminLog('后台登录', __ACTION__);
                    $url = session('from_url') ? session('from_url') : U('Admin/Index/index');
                    exit(json_encode(array('status' => 1, 'url' => $url)));
                } else {
                    exit(json_encode(array('status' => 0, 'msg' => '账号密码不正确')));
                }
            } else {
                exit(json_encode(array('status' => 0, 'msg' => '请填写账号密码')));
            }
        }
        $this->display();
    }

    /**
     * 验证码获取
     */
    public function vertify() {
        $config = array(
            'fontSize' => 30,
            'length' => 4,
            'useCurve' => true,
            'useNoise' => false,
            'reset' => false
        );
        $Verify = new Verify($config);
        $Verify->entry("admin_login");
    }

}
